﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WyUnity.Utility;

namespace RFID_Interface
{

    public static class Util
    {
        private static Logger _log = Log.Get("RFID_Interface.Util");
        public static DateTime GetDateFromBarcode(string barcode)
        {
            try
            {
                DateTime rlt;
                var datestr = barcode.Substring(5, 6);
                rlt = new DateTime(2000 + int.Parse(datestr.Substring(0, 2)), int.Parse(datestr.Substring(2, 2)), int.Parse(datestr.Substring(4, 2)));

                return rlt;
            }
            catch
            {
                _log.Error("从条码中获取生产日期失败。" + barcode);
                throw new ServiceException("条码格式不正确,{0}", barcode);
            }
        }

        /// <summary>
        /// 从条码中获取生产线编码
        /// </summary>
        internal static string GetProductLineFromBarcode(string barcode)
        {
            var pline = string.Empty;

            //17位传统条码或整笼条码
            //format: xxxxx yymmdd pp w sss (Z)
            if (barcode.Length == 17 || (barcode.Length == 18 && barcode.Substring(17, 1) == "Z"))
            {
                pline = barcode.Substring(11, 2);
            }

            return pline;
        }

        /// <summary>
        /// 从条码中获取班组名称
        /// </summary>
        internal static string GetWorkClassFromBarcode(string barcode)
        {

            var workclass = string.Empty;

            //17位传统条码或整笼条码
            //format: xxxxx yymmdd pp w sss (Z)
            if (barcode.Length == 17 || (barcode.Length == 18 && barcode.Substring(17, 1) == "Z"))
            {
                workclass = barcode.Substring(13, 1);
            }

            return workclass;
        }
    }
}